package model.proxy
{
	import model.Utilisateur;
	
	import mx.collections.ArrayCollection;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.remoting.mxml.RemoteObject;
	
	import org.puremvc.as3.interfaces.IProxy;
	import org.puremvc.as3.patterns.proxy.Proxy; 
	
	public class DroitUtilisateurProxy extends Proxy implements IProxy 
	{ 
		
		public static const NAME : String = 'DroitUtilisateurProxy';		
		public static const LISTE_USERS_OBTENUE : String = "ListeUsersObtenue";
		public static const LISTE_ALL_AN_ETUDE_OBTENUE : String = "ListeAllAnEtudeObtenue";
		public static const USER_OBTENU : String = "UserObtenu";
		
		private var remoteObject : RemoteObject = null;
		private var remoteObject2 : RemoteObject = null;
		
		/** 
		 * Constructor 
		 * We create the table logins and insert a login by default to test 
		 */ 
		public function DroitUtilisateurProxy(data:Object=null) 
		{ 
			super(NAME, data);
			
			remoteObject = new RemoteObject();
			remoteObject.destination = "RemoteServiceUtilisateur";
			remoteObject.source = "RemoteServiceUtilisateur";
			remoteObject.endpoint = ApplicationFacade.SERVEUR;
			
			remoteObject2 = new RemoteObject();
			remoteObject2.destination = "RemoteServiceAnneeEtude";
			remoteObject2.source = "RemoteServiceAnneeEtude";
			remoteObject2.endpoint = ApplicationFacade.SERVEUR;
		} 
 	
 		public function obtenirListeUsers() : void 
 		{
 			remoteObject.addEventListener(ResultEvent.RESULT, listeUsersObtenue);
			remoteObject.addEventListener(FaultEvent.FAULT, onFault);
 			remoteObject.obtenirListeUtilisateurs();
 		}
 		
 		public function obtenirListeAllAnEtude() : void 
 		{
 			remoteObject2.addEventListener(ResultEvent.RESULT, listeAllAnEtudeObtenue);
			remoteObject2.addEventListener(FaultEvent.FAULT, onFault);
 			remoteObject2.obtenirListeAnneesEtudeAll();
 		}
 		
 		public function modifierUtilisateur(user:Utilisateur, collAnEtude: ArrayCollection) : void 
 		{
 			remoteObject.addEventListener(ResultEvent.RESULT, userObtenu);
			remoteObject.addEventListener(FaultEvent.FAULT, onFault);
 			remoteObject.modifierUtilisateur(user, collAnEtude);
 		}
 		
 		private function listeUsersObtenue(event:ResultEvent):void
		{
			var listeUsers:Array = (event.result as Array)
			sendNotification(LISTE_USERS_OBTENUE, listeUsers);
		}
		
		private function listeAllAnEtudeObtenue(event:ResultEvent):void
		{
			var listeAnEtude:Array = (event.result as Array);
			sendNotification(LISTE_ALL_AN_ETUDE_OBTENUE, listeAnEtude);
		}
		
		private function userObtenu(event:ResultEvent):void
		{
			var users:Array = (event.result as Array);
			sendNotification(USER_OBTENU, users);
		}
		
		private function onFault(event:FaultEvent):void
		{
			trace("Fault");			
		}
	}
}